#include "StdAfx.h"
#include "YoYoFactory.h"
#include "sgcomposite.h"
#include "sgcylinder.h"
#include "sgpolygon.h"
#include "sgoctagonalcylinder.h"
#include "sgrotate.h"
#include "sgrotatearoundpoint.h"
#include "sgmesh.h"
#include "sgmaterial.h"
#include "sgtranslate.h"
#include "sgtexture.h"
#include "sgptr.h"
#include "sgseparator.h"

#define PI 3.1415926

YoYoFactory::YoYoFactory(void)
{
	m_ride = new CSgComposite();

	m_towerTexture = new CTexture();
	m_towerTexture->LoadFile(L"towertexture.bmp");
	m_yoyologo = new CTexture();
	m_yoyologo->LoadFile(L"yoyotexture.bmp");
	m_carnivalside = new CTexture();
	m_carnivalside->LoadFile(L"side.bmp");
	m_happy = new CTexture();
	m_happy->LoadFile(L"happyf.bmp");
	m_ChairRotation = 0;
	m_BeamRotation = 0;
	m_SpinRotation = 0;
	m_TopSpin = 0;
}

YoYoFactory::~YoYoFactory(void)
{
	delete m_yoyologo;
	delete m_towerTexture;
}
/*
use fmod()

double YoYoFactory::modulus(double a, double b)
{
	int result = static_cast<int>( a / b );
	return a - static_cast<double>( result ) * b;
}
*/
void YoYoFactory::AdvanceTime(const DWORD & timeDiff, const DWORD & currentTime)
{
	// time in SECONDS
	float temp = (currentTime/1000)%30;
	// Time since the last 5 second inteval
	float temp2 = float(fmod(fmod((currentTime/1000.0),30.0),5.0));
	// Time since the last 15 second inteval
	float temp3 = float(fmod(fmod((currentTime/1000.0),30.0)+10.0,15.0));
	if (temp < 5) // beginning spin
	{
		m_ChairRotation = -15.0f * float(sin(temp2*(PI/2.0f)/5.0f));
		m_BeamRotation = 0.0f;
		m_SpinRotation = float(-sin((5.0f-temp2)/5.0f * (PI/2.0f))) * 180.0f;
	}
	else if (temp < 20) // spinning at full speed, raise/lower beams & seats
	{
		m_ChairRotation = -15.0f * float(sin(temp3*(PI)/15.0f)) - 15.0f;
		m_BeamRotation = -10.0f * float(sin(temp3*(PI)/15.0f));
		m_SpinRotation = temp3/5.0f * 270.0f;
	}
	else if (temp < 25) // slowing down
	{
		m_ChairRotation = -15.0f * float(cos(temp2*(PI/2.0f)/5.0f));
		m_BeamRotation = 0.0f;
		m_SpinRotation = float(sin(temp2/5.0f * (PI/2.0f))) * 180.0f;
	}
	else // stopped
	{
		m_ChairRotation = 0.0f;
		m_BeamRotation = 0.0f;
		m_SpinRotation = 0.0f;
	}
	m_TopSpin = fmod((currentTime/1000.0),6.0) * 60.0f;

	// actually update the objects in time for the next Render()
	for (int k=0; k < 16; k++)
	{
		m_rotate_point_rpt[k]->SetDegrees(m_BeamRotation);
		m_rotate_point_r2[k]->SetDegrees(22.5*k + m_SpinRotation);
		m_rotate_point_rpt3[k]->SetDegrees(m_ChairRotation);
		m_rotate_point_rpt2[k]->SetDegrees(m_BeamRotation);
		m_rotate_point_r3[k]->SetDegrees(22.5*k+m_SpinRotation);
		m_rotate_point_rpt8[k]->SetDegrees(m_ChairRotation);
		m_rotate_point_rpt6[k]->SetDegrees(m_BeamRotation);
		m_rotate_point_r4[k]->SetDegrees(22.5/2.0f*(2*k+1)+m_SpinRotation);
		m_rotate_point_r10[k]->SetDegrees(m_TopSpin);
	}
}

CSgObject* YoYoFactory::Create()
{
	/**************************
	// TOWER
	**************************/
	CSgPtr<CSgComposite> m_tower = MakeTower();
	m_ride->AddChild(m_tower);


	/*********************
	// CREATE TOP
	*********************/
	CSgPtr<CSgComposite> topper = MakeTop();
	CSgPtr<CSgTranslate> transv = new CSgTranslate(0.0,7.25,0.0);
	transv->SetChild(topper);

	// DECLARATION FOR TOP TOWER TO BE USED LATER ON
	CSgPtr<CSgRotate> r10 = new CSgRotate(m_TopSpin,0,1,0);	

	/**************************
	// BEAMS, SEATS, AND CHAINS
	**************************/
	CSgPtr<CSgComposite> m_seat = MakeSeat();
	CSgPtr<CSgMaterial> greenseat = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	CSgPtr<CSgMaterial> yellowseat = new CSgMaterial(50.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f);
	greenseat->SetChild(m_seat);
	yellowseat->SetChild(m_seat);


	CSgPtr<CSgComposite> m_chains = MakeChains(0);
	CSgPtr<CSgComposite> m_chains_offset = MakeChains(1);

	CSgPtr<CSgMaterial> chainmat1 = new CSgMaterial(50.0f, 0.549f, 0.549f, 0.549f, 1.0f, 0.549f, 0.549f, 0.549f, 1.0f);
	CSgPtr<CSgMaterial> chainmat2 = new CSgMaterial(50.0f, 0.549f, 0.549f, 0.549f, 1.0f, 0.549f, 0.549f, 0.549f, 1.0f);
	chainmat1->SetChild(m_chains);
	chainmat2->SetChild(m_chains_offset);

	CSgPtr<CSgComposite> m_beam = MakeBeams();

	

	for (int k=0; k < 16; k++)
	{
		/**************************
		// MAKE BEAMS
		**************************/
		CSgPtr<CSgTranslate> tra = new CSgTranslate(-2.75f,7.2f,0.0f);
		tra->SetChild(m_beam);
		CSgPtr<CSgRotateAroundPoint> rpt = new CSgRotateAroundPoint(m_BeamRotation,-.75, 7.2,0,0,0,1);
		//rotation
		m_rotate_point_rpt[k] = rpt;
		
		rpt->SetChild(tra);
		CSgPtr<CSgRotate> r2 = new CSgRotate(22.5*k + m_SpinRotation,0,1,0);
		m_rotate_point_r2[k] = r2;

		r2->SetChild(rpt);
		CSgPtr<CSgSeparator> sep = new CSgSeparator();
		sep->SetChild(r2);

		m_ride->AddChild(sep);
		
		/**************************
		// MAKE SEATS
		**************************/
		// define composite for both seats & chains
		CSgPtr<CSgComposite> m_seatAndChains = new CSgComposite();
		CSgPtr<CSgComposite> m_seatAndChains_offset = new CSgComposite();

		/*********************
		// CREATE MAIN SEAT
		*********************/
		m_seatAndChains->AddChild(yellowseat);
		m_seatAndChains->AddChild(chainmat1);		

		// seat 1
		
		CSgPtr<CSgRotateAroundPoint> rpt3 = new CSgRotateAroundPoint(m_ChairRotation,0, 6.35,0,0,0,1); // chair rot
		m_rotate_point_rpt3[k] = rpt3;

		rpt3->SetChild(m_seatAndChains);
		CSgPtr<CSgTranslate> tr3 = new CSgTranslate(-5.5f,1.0f,0.0f);
		tr3->SetChild(rpt3);
		CSgPtr<CSgRotateAroundPoint> rpt2 = new CSgRotateAroundPoint(m_BeamRotation,-.75, 7.2,0,0,0,1); // beam rot
		m_rotate_point_rpt2[k] = rpt2;

		rpt2->SetChild(tr3);
		CSgPtr<CSgRotate> r3 = new CSgRotate(22.5*k+m_SpinRotation,0,1,0);
		m_rotate_point_r3[k] = r3;

		r3->SetChild(rpt2);		
		CSgPtr<CSgSeparator> sep2 = new CSgSeparator();
		sep2->SetChild(r3);

		m_ride->AddChild(sep2);

		/*********************
		// CREATE OFFSET SEAT
		*********************/
		m_seatAndChains_offset->AddChild(greenseat);
		m_seatAndChains_offset->AddChild(chainmat2);

		// seat 2 (offset seat)
		// rotate main beam & seat.
		CSgPtr<CSgRotateAroundPoint> rpt8 = new CSgRotateAroundPoint(m_ChairRotation,0, 6.35,0,0,0,1); // chair rot
		m_rotate_point_rpt8[k] = rpt8;

		rpt8->SetChild(m_seatAndChains_offset);
		CSgPtr<CSgTranslate> tr4 = new CSgTranslate(-5.46f, 1.0f, 0.0f);
		tr4->SetChild(rpt8);
		CSgPtr<CSgRotateAroundPoint> rpt6 = new CSgRotateAroundPoint(m_BeamRotation,-0.75, 7.2, 0,0,0,1); // beam rot
		m_rotate_point_rpt6[k] = rpt6;

		rpt6->SetChild(tr4);
		CSgPtr<CSgRotate> r4 = new CSgRotate(22.5/2.0f*(2*k+1)+m_SpinRotation,0,1,0);
		m_rotate_point_r4[k] = r4;

		r4->SetChild(rpt6);
		CSgPtr<CSgSeparator> sep3 = new CSgSeparator();
		sep3->SetChild(r4);

		m_ride->AddChild(sep3);

		
		// SPIN THE TOP
		m_rotate_point_r10[k] = r10;
	}
	
	/*********************
	// SPIN TOP
	*********************/
	
	r10->SetChild(transv);
	CSgPtr<CSgSeparator> sep45 = new CSgSeparator();
	sep45->SetChild(r10);
	m_ride->AddChild(sep45);

	/*CSgPtr<CSgMaterial> nope = new CSgMaterial(10.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
	m_ride->AddChild(nope);*/

	return m_ride;
}


CSgPtr<CSgComposite> YoYoFactory::MakeTower()
{
	CSgPtr<CSgComposite> m_tower = new CSgComposite();


	CSgPtr<CSgMaterial> baseMmat = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	CSgPtr<CSgOctagonalCylinder> baseM = new CSgOctagonalCylinder(1.5f,3.0f, m_carnivalside, baseMmat);	
	CSgPtr<CSgTranslate> tr1 = new CSgTranslate(0.0f,0.75f,0.0f);
	tr1->SetChild(baseM);
	CSgPtr<CSgSeparator> sep1 = new CSgSeparator();
	sep1->SetChild(tr1);
	

	m_tower->AddChild(sep1);

	CSgPtr<CSgMaterial> base2mat = new CSgMaterial(50.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f);
	CSgPtr<CSgOctagonalCylinder> base2 = new CSgOctagonalCylinder(2.0f,1.0f, m_towerTexture, base2mat);
	// shiny, diffuse, specular rgba
	// green: 0.1255f, 0.0753f, 0.251f, 1.0f,
	// yel: 1.0f, 1.0f, 0.0f, 1.0f,
	////CSgPtr<CSgMaterial> botmat = new CSgMaterial(50.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f);


	CSgPtr<CSgTranslate> tr2 = new CSgTranslate(0.0f,2.5f,0.0f);
	tr2->SetChild(base2);
	CSgPtr<CSgSeparator> sep2 = new CSgSeparator();
	sep2->SetChild(tr2);
	m_tower->AddChild(sep2);

	GLdouble basetop1[] = {0.0,	2.5,	0.85};
	GLdouble basetop2[] = {0.78,	2.5,	-0.45};
	GLdouble basetop3[] = {-0.78,	2.5,	-0.45};

	GLdouble basebot1[] = {0.0,	0.0,	0.85};
	GLdouble basebot2[] = {0.78,	0.0,	-0.45};
	GLdouble basebot3[] = {-0.78,	0.0,	-0.45};

	CSgPtr<CSgComposite> triangularPrism = new CSgComposite();
	// mid triangle tower top
	CSgPtr<CSgPolygon> sideT = new CSgPolygon();
	sideT->AddNormal(CGrVector(0, 1, 0));
	sideT->AddVertex(CGrVector(basetop1));
	sideT->AddVertex(CGrVector(basetop2));
	sideT->AddVertex(CGrVector(basetop3));
	CSgPtr<CSgMaterial> toptrimat = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	toptrimat->SetChild(sideT);
	CSgPtr<CSgSeparator> separator = new CSgSeparator();
	separator->SetChild(toptrimat);
	triangularPrism->AddChild(separator);

	// mid triangle tower bottom
	CSgPtr<CSgPolygon> sideB = new CSgPolygon();
	sideB->AddNormal(CGrVector(0, 1, 0));
	sideB->AddVertex(CGrVector(basebot3));
	sideB->AddVertex(CGrVector(basebot1));
	sideB->AddVertex(CGrVector(basebot2));
	triangularPrism->AddChild(sideB);

	// mid triangle tower side1
	CSgPtr<CSgPolygon> side1 = new CSgPolygon();
	side1->AddNormal(CGrVector(0.87575, 0, 0.5145));
	side1->AddTexCoord(CGrVector(0, 1));
	side1->AddVertex(CGrVector(basetop2));
	side1->AddTexCoord(CGrVector(1, 1));
	side1->AddVertex(CGrVector(basetop1));
	side1->AddTexCoord(CGrVector(1, 0));
	side1->AddVertex(CGrVector(basebot1));
	side1->AddTexCoord(CGrVector(0, 0));
	side1->AddVertex(CGrVector(basebot2));	

	CSgPtr<CSgTexture> tex1 = new CSgTexture(m_yoyologo);
	tex1->SetChild(side1);
	triangularPrism->AddChild(tex1);

	// mid triangle tower side2
	CSgPtr<CSgPolygon> side2 = new CSgPolygon();
	side2->AddNormal(CGrVector(0, 0, -1));
	side2->AddTexCoord(CGrVector(0, 1));
	side2->AddVertex(CGrVector(basetop3));
	side2->AddTexCoord(CGrVector(1, 1));
	side2->AddVertex(CGrVector(basetop2));
	side2->AddTexCoord(CGrVector(1, 0));
	side2->AddVertex(CGrVector(basebot2));
	side2->AddTexCoord(CGrVector(0, 0));
	side2->AddVertex(CGrVector(basebot3));

	CSgPtr<CSgTexture> tex3 = new CSgTexture(m_yoyologo);
	tex3->SetChild(side2);
	triangularPrism->AddChild(tex3);

	// mid triangle tower side3
	CSgPtr<CSgPolygon> side3 = new CSgPolygon();
	side3->AddNormal(CGrVector(-0.87575, 0, 0.5145));
	side3->AddTexCoord(CGrVector(0, 1));
	side3->AddVertex(CGrVector(basetop1));
	side3->AddTexCoord(CGrVector(1, 1));
	side3->AddVertex(CGrVector(basetop3));
	side3->AddTexCoord(CGrVector(1, 0));
	side3->AddVertex(CGrVector(basebot3));
	side3->AddTexCoord(CGrVector(0, 0));
	side3->AddVertex(CGrVector(basebot1));

	CSgPtr<CSgTexture> tex2 = new CSgTexture(m_yoyologo);
	tex2->SetChild(side3);
	triangularPrism->AddChild(tex2);

	
	CSgPtr<CSgTranslate> tr5 = new CSgTranslate(0.0f,3.5f,0.0f);
	tr5->SetChild(triangularPrism);
	CSgPtr<CSgSeparator> sep5 = new CSgSeparator();
	sep5->SetChild(tr5);
	m_tower->AddChild(sep5);

	// replace with mesh cylinder?
	CSgPtr<CSgCylinder> baseMid = new CSgCylinder(1.0f,0.3f);
	CSgPtr<CSgMaterial> topmat = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	topmat->SetChild(baseMid);
	CSgPtr<CSgTranslate> tr4 = new CSgTranslate(0.0f,6.0f,0.0f);
	tr4->SetChild(topmat);
	CSgPtr<CSgSeparator> sep4 = new CSgSeparator();
	sep4->SetChild(tr4);
	m_tower->AddChild(sep4);
	// shiny, diffuse, specular rgba
	// green: 0.1255f, 0.0753f, 0.251f, 1.0f,
	// yel: 1.0f, 1.0f, 0.0f, 1.0f,
	////CSgPtr<CSgMaterial> botmat = new CSgMaterial(50.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f);

	
	
	CSgPtr<CSgOctagonalCylinder> base3 = new CSgOctagonalCylinder(0.25f,1.0f);
	CSgPtr<CSgMaterial> matCyl = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	matCyl->SetChild(baseMid);
	CSgPtr<CSgTranslate> tr3 = new CSgTranslate(0.0f,7.125f,0.0f);
	tr3->SetChild(base3);
	CSgPtr<CSgSeparator> sep3 = new CSgSeparator();
	sep3->SetChild(tr3);
	m_tower->AddChild(sep3);

	return m_tower;
}

CSgPtr<CSgComposite> YoYoFactory::MakeBeams()
{
	CSgPtr<CSgComposite> m_beam_full = new CSgComposite();

	CSgPtr<CSgComposite> m_beam = MakeBeam(4.0f,0.2f,0.4f);
	CSgPtr<CSgMaterial> beamLongMat = new CSgMaterial(50.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f);
	beamLongMat->SetChild(m_beam);
	CSgPtr<CSgSeparator> separator2 = new CSgSeparator();
	separator2->SetChild(beamLongMat);
	m_beam_full->AddChild(separator2);


	CSgPtr<CSgComposite> m_outcast_beam = MakeBeam(0.75f,0.1f,0.1f);
	CSgPtr<CSgComposite> m_outcast_beam2 = MakeBeam(0.75f,0.1f,0.1f);

	CSgPtr<CSgTranslate> trans = new CSgTranslate(-2.375f,0.1f,0.0f);
	trans->SetChild(m_outcast_beam);
	CSgPtr<CSgSeparator> sep1 = new CSgSeparator();
	sep1->SetChild(trans);

	CSgPtr<CSgRotate> rot = new CSgRotate(90.0f,0,1,0);
	rot->SetChild(m_outcast_beam2);
	CSgPtr<CSgTranslate> trans2 = new CSgTranslate(-2.75f,0.1f,0.0f);
	trans2->SetChild(rot);
	CSgPtr<CSgSeparator> sep2 = new CSgSeparator();
	sep2->SetChild(trans2);


	/*******************
	Stupid curved beam
	********************/

	CSgPtr<CSgComposite> m_curvedBeam = new CSgComposite();	
	CSgPtr<CSgMesh> m_cbst = new CSgMesh();
	CSgPtr<CSgMesh> m_cbsl = new CSgMesh();
	CSgPtr<CSgMesh> m_cbsr = new CSgMesh();
	CSgPtr<CSgMesh> m_cbsb = new CSgMesh();

	CGrVector outpt(0.375,	0.25,	0.05);
	CGrVector inpt(0.32,	0.2,	-0.05);

	CGrVector prevptH(outpt.X(),0.0, outpt.Z());

	CGrVector vertex(outpt.X(),0.0, outpt.Z());
	m_cbst->AddVertex( vertex );
	m_cbsl->AddVertex( vertex );
	vertex[2] *= -1;
	m_cbst->AddVertex( vertex );
	m_cbsr->AddVertex( vertex );

	CGrVector prevptL(inpt.X(), 0.0, inpt.Z());
	vertex = CGrVector(inpt.X(), 0.0, inpt.Z());
	m_cbsr->AddVertex( vertex );
	m_cbsb->AddVertex( vertex );
	vertex[2] *= -1;
	m_cbsb->AddVertex( vertex );
	m_cbsl->AddVertex( vertex );


	m_cbsl->AddNormal(CGrVector(0,0,1));
	m_cbsr->AddNormal(CGrVector(0,0,-1));

	for (int i=1;i<6;i++)
	{
		double angle = (PI)/5;

		vertex = CGrVector(outpt.X() * cos(angle * i), outpt.Y() * sin(angle * i), outpt.Z());
		m_cbst->AddVertex( vertex );
		m_cbsl->AddVertex( vertex );

	
		CGrVector normalT = Cross3(CGrVector( prevptH - CGrVector(prevptH.X(),prevptH.Y(),-prevptH.Z()) ), CGrVector( prevptH - vertex));
		normalT.Normalize3();
		m_cbst->AddNormal( normalT );

		

		vertex[2] *= -1;
		m_cbst->AddVertex( vertex );
		m_cbsr->AddVertex( vertex );
		

		m_cbst->AddTriangleVertex(2*(i-1)+3, i-1, -1);
		m_cbst->AddTriangleVertex(2*(i-1)+2, i-1, -1);
		m_cbst->AddTriangleVertex(2*(i-1)+1, i-1, -1);
		m_cbst->AddTriangleVertex(2*(i-1)  , i-1, -1);
		m_cbst->AddTriangleVertex(2*(i-1)+1, i-1, -1);
		m_cbst->AddTriangleVertex(2*(i-1)+2, i-1, -1);



		CGrVector vertex(inpt.X() * cos(angle * i), inpt.Y() * sin(angle * i), inpt.Z());
		m_cbsr->AddVertex( vertex );
		m_cbsb->AddVertex( vertex );

		m_cbsr->AddTriangleVertex(2*(i-1)+3, 0, -1);
		m_cbsr->AddTriangleVertex(2*(i-1)+2, 0, -1);
		m_cbsr->AddTriangleVertex(2*(i-1)+1, 0, -1);
		m_cbsr->AddTriangleVertex(2*(i-1)  , 0, -1);
		m_cbsr->AddTriangleVertex(2*(i-1)+1, 0, -1);
		m_cbsr->AddTriangleVertex(2*(i-1)+2, 0, -1);



		vertex[2] *= -1;
		m_cbsb->AddVertex( vertex );
		m_cbsl->AddVertex( vertex );

		CGrVector normalB = Cross3(CGrVector( prevptL - CGrVector(prevptL.X(),prevptL.Y(),-prevptL.Z()) ), CGrVector( prevptL - vertex));
		normalB.Normalize3();
		m_cbsb->AddNormal( normalB );

		m_cbsb->AddTriangleVertex(2*(i-1)+3, i-1, -1);
		m_cbsb->AddTriangleVertex(2*(i-1)+2, i-1, -1);
		m_cbsb->AddTriangleVertex(2*(i-1)+1, i-1, -1);
		m_cbsb->AddTriangleVertex(2*(i-1)  , i-1, -1);
		m_cbsb->AddTriangleVertex(2*(i-1)+1, i-1, -1);
		m_cbsb->AddTriangleVertex(2*(i-1)+2, i-1, -1);


		m_cbsl->AddTriangleVertex(2*(i-1)+1, 0, -1);
		m_cbsl->AddTriangleVertex(2*(i-1)+2, 0, -1);
		m_cbsl->AddTriangleVertex(2*(i-1)+3, 0, -1);
		m_cbsl->AddTriangleVertex(2*(i-1)+2, 0, -1);
		m_cbsl->AddTriangleVertex(2*(i-1)+1, 0, -1);
		m_cbsl->AddTriangleVertex(2*(i-1)  , 0, -1);

		prevptH = CGrVector(outpt.X() * cos(angle * i), outpt.Y() * sin(angle * i), outpt.Z());
		prevptL = CGrVector(inpt.X() * cos(angle * i), inpt.Y() * sin(angle * i), inpt.Z());

	}
	m_curvedBeam->AddChild(m_cbsr);
	m_curvedBeam->AddChild(m_cbsb);
	m_curvedBeam->AddChild(m_cbsl);
	m_curvedBeam->AddChild(m_cbst);

	CSgPtr<CSgRotate> rot9 = new CSgRotate(90.0f,0,1,0);
	rot9->SetChild(m_curvedBeam);
	CSgPtr<CSgTranslate> trans6 = new CSgTranslate(-2.75f,0.2f,0.0f);
	trans6->SetChild(rot9);
	CSgPtr<CSgSeparator> sep4 = new CSgSeparator();
	sep4->SetChild(trans6);



	CSgPtr<CSgComposite> m_oBeams = new CSgComposite();
	m_oBeams->AddChild(sep1);
	m_oBeams->AddChild(sep2);
	m_oBeams->AddChild(sep4);

	CSgPtr<CSgMaterial> beamColor = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	beamColor->SetChild(m_oBeams);
	CSgPtr<CSgSeparator> separator3 = new CSgSeparator();
	separator3->SetChild(beamColor);

	m_beam_full->AddChild(separator3);
	
	return m_beam_full;
}

CSgPtr<CSgComposite> YoYoFactory::MakeBeam(float length, float height, float width)
{
	CSgPtr<CSgComposite> m_outcast_beam = new CSgComposite();

	GLdouble beamtop[] = {length/2.0f,	height,	width/2.0f};
	GLdouble beamtop2[] = {length/2.0f,	height,	-width/2.0f};
	GLdouble beamtop3[] = {-length/2.0f,	height,	-width/2.0f};
	GLdouble beamtop4[] = {-length/2.0f,	height,	width/2.0f};
	GLdouble beambot[] = {length/2.0f,	0.0,	width/2.0f};
	GLdouble beambot2[] = {length/2.0f,	0.0,	-width/2.0f};
	GLdouble beambot3[] = {-length/2.0f,	0.0,	-width/2.0f};
	GLdouble beambot4[] = {-length/2.0f,	0.0,	width/2.0f};

	// top
	CSgPtr<CSgMesh> top2 = new CSgMesh();
	top2->AddNormal(CGrVector(0,1,0));
	top2->AddVertex(CGrVector(beamtop));
	top2->AddVertex(CGrVector(beamtop2));
	top2->AddVertex(CGrVector(beamtop3));
	top2->AddVertex(CGrVector(beamtop4));
	top2->AddTriangleVertex(0,0,-1);
	top2->AddTriangleVertex(1,0,-1);
	top2->AddTriangleVertex(2,0,-1);
	top2->AddTriangleVertex(2,0,-1);
	top2->AddTriangleVertex(3,0,-1);
	top2->AddTriangleVertex(0,0,-1);
	m_outcast_beam->AddChild(top2);

	// bot
	CSgPtr<CSgMesh> bot2 = new CSgMesh();
	bot2->AddNormal(CGrVector(0,-1,0));
	bot2->AddVertex(CGrVector(beambot));
	bot2->AddVertex(CGrVector(beambot4));
	bot2->AddVertex(CGrVector(beambot3));
	bot2->AddVertex(CGrVector(beambot2));
	bot2->AddTriangleVertex(0,0,-1);
	bot2->AddTriangleVertex(1,0,-1);
	bot2->AddTriangleVertex(2,0,-1);
	bot2->AddTriangleVertex(2,0,-1);
	bot2->AddTriangleVertex(3,0,-1);
	bot2->AddTriangleVertex(0,0,-1);
	m_outcast_beam->AddChild(bot2);

	//sides
	CSgPtr<CSgMesh> side5 = new CSgMesh();
	side5->AddNormal(CGrVector(1,0,0));
	side5->AddVertex(CGrVector(beamtop2));
	side5->AddVertex(CGrVector(beamtop));
	side5->AddVertex(CGrVector(beambot));
	side5->AddVertex(CGrVector(beambot2));
	side5->AddTriangleVertex(0,0,-1);
	side5->AddTriangleVertex(1,0,-1);
	side5->AddTriangleVertex(2,0,-1);
	side5->AddTriangleVertex(2,0,-1);
	side5->AddTriangleVertex(3,0,-1);
	side5->AddTriangleVertex(0,0,-1);
	m_outcast_beam->AddChild(side5);

	CSgPtr<CSgMesh> side6 = new CSgMesh();
	side6->AddNormal(CGrVector(0,0,-1));
	side6->AddVertex(CGrVector(beamtop3));
	side6->AddVertex(CGrVector(beamtop2));
	side6->AddVertex(CGrVector(beambot2));
	side6->AddVertex(CGrVector(beambot3));
	side6->AddTriangleVertex(0,0,-1);
	side6->AddTriangleVertex(1,0,-1);
	side6->AddTriangleVertex(2,0,-1);
	side6->AddTriangleVertex(2,0,-1);
	side6->AddTriangleVertex(3,0,-1);
	side6->AddTriangleVertex(0,0,-1);
	m_outcast_beam->AddChild(side6);

	CSgPtr<CSgMesh> side7 = new CSgMesh();
	side7->AddNormal(CGrVector(-1,0,0));
	side7->AddVertex(CGrVector(beamtop4));
	side7->AddVertex(CGrVector(beamtop3));
	side7->AddVertex(CGrVector(beambot3));
	side7->AddVertex(CGrVector(beambot4));
	side7->AddTriangleVertex(0,0,-1);
	side7->AddTriangleVertex(1,0,-1);
	side7->AddTriangleVertex(2,0,-1);
	side7->AddTriangleVertex(2,0,-1);
	side7->AddTriangleVertex(3,0,-1);
	side7->AddTriangleVertex(0,0,-1);
	m_outcast_beam->AddChild(side7);

	CSgPtr<CSgMesh> side8 = new CSgMesh();
	side8->AddNormal(CGrVector(0,0,1));
	side8->AddVertex(CGrVector(beamtop));
	side8->AddVertex(CGrVector(beamtop4));
	side8->AddVertex(CGrVector(beambot4));
	side8->AddVertex(CGrVector(beambot));
	side8->AddTriangleVertex(0,0,-1);
	side8->AddTriangleVertex(1,0,-1);
	side8->AddTriangleVertex(2,0,-1);
	side8->AddTriangleVertex(2,0,-1);
	side8->AddTriangleVertex(3,0,-1);
	side8->AddTriangleVertex(0,0,-1);
	m_outcast_beam->AddChild(side8);

	return m_outcast_beam;
}


CSgPtr<CSgComposite> YoYoFactory::MakeSeat()
{
	CSgPtr<CSgComposite> m_seats = new CSgComposite();
	
	GLdouble seattop1[] = {-0.25,	0.025,	-0.25};
	GLdouble seattop2[] = {-0.25,	0.025,	0.25};
	GLdouble seattop3[] = {0.25,	0.025,	0.25};
	GLdouble seattop4[] = {0.25,	0.025,	-0.25};

	GLdouble seatbot1[] = {-0.25,	0.,	-0.25};
	GLdouble seatbot2[] = {-0.25,	0.,	0.25};
	GLdouble seatbot3[] = {0.25,	0.,	0.25};
	GLdouble seatbot4[] = {0.25,	0.,	-0.25};

	GLdouble seatback1[] = {-0.25,	0.45,	-0.25};
	GLdouble seatback2[] = {0.25,	0.45,	-0.25};
	GLdouble seatback3[] = {-0.25,	0.45,	-0.275};
	GLdouble seatback4[] = {0.25,	0.45,	-0.275};
	GLdouble seatback5[] = {-0.25,	0.,	-0.275};
	GLdouble seatback6[] = {0.25,	0.,	-0.275};

	CSgPtr<CSgMesh> top = new CSgMesh();
	top->AddNormal(CGrVector(0, 1, 0));
	top->AddVertex(CGrVector(seattop1));
	top->AddVertex(CGrVector(seattop2));
	top->AddVertex(CGrVector(seattop3));
	top->AddVertex(CGrVector(seattop4));
	top->AddTriangleVertex(0,0,-1);
	top->AddTriangleVertex(1,0,-1);
	top->AddTriangleVertex(2,0,-1);
	top->AddTriangleVertex(2,0,-1);
	top->AddTriangleVertex(3,0,-1);
	top->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(top);

	CSgPtr<CSgMesh> bottom = new CSgMesh();
	bottom->AddNormal(CGrVector(0, -1, 0));
	bottom->AddVertex(CGrVector(seatbot1));
	bottom->AddVertex(CGrVector(seatback5));
	bottom->AddVertex(CGrVector(seatback6));
	bottom->AddVertex(CGrVector(seatbot4));
	bottom->AddVertex(CGrVector(seatbot3));
	bottom->AddVertex(CGrVector(seatbot2));
	bottom->AddTriangleVertex(1,0,-1);
	bottom->AddTriangleVertex(2,0,-1);
	bottom->AddTriangleVertex(4,0,-1);
	bottom->AddTriangleVertex(4,0,-1);
	bottom->AddTriangleVertex(5,0,-1);
	bottom->AddTriangleVertex(1,0,-1);
	m_seats->AddChild(bottom);

	CSgPtr<CSgMesh> sideF = new CSgMesh();
	sideF->AddNormal(CGrVector(0, 0, 1));
	sideF->AddVertex(CGrVector(seattop3));
	sideF->AddVertex(CGrVector(seattop2));
	sideF->AddVertex(CGrVector(seatbot2));
	sideF->AddVertex(CGrVector(seatbot3));
	sideF->AddTriangleVertex(0,0,-1);
	sideF->AddTriangleVertex(1,0,-1);
	sideF->AddTriangleVertex(2,0,-1);
	sideF->AddTriangleVertex(2,0,-1);
	sideF->AddTriangleVertex(3,0,-1);
	sideF->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(sideF);

	CSgPtr<CSgMesh> sideL = new CSgMesh();
	sideL->AddNormal(CGrVector(-1, 0, 0));
	sideL->AddVertex(CGrVector(seattop2));
	sideL->AddVertex(CGrVector(seattop1));
	sideL->AddVertex(CGrVector(seatbot1));
	sideL->AddVertex(CGrVector(seatbot2));
	sideL->AddTriangleVertex(0,0,-1);
	sideL->AddTriangleVertex(1,0,-1);
	sideL->AddTriangleVertex(2,0,-1);
	sideL->AddTriangleVertex(2,0,-1);
	sideL->AddTriangleVertex(3,0,-1);
	sideL->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(sideL);

	CSgPtr<CSgMesh> sideR = new CSgMesh();
	sideR->AddNormal(CGrVector(1, 0, 0));
	sideR->AddVertex(CGrVector(seattop4));
	sideR->AddVertex(CGrVector(seattop3));
	sideR->AddVertex(CGrVector(seatbot3));
	sideR->AddVertex(CGrVector(seatbot4));
	sideR->AddTriangleVertex(0,0,-1);
	sideR->AddTriangleVertex(1,0,-1);
	sideR->AddTriangleVertex(2,0,-1);
	sideR->AddTriangleVertex(2,0,-1);
	sideR->AddTriangleVertex(3,0,-1);
	sideR->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(sideR);

	

	// back rest
	CSgPtr<CSgMesh> backF = new CSgMesh();
	backF->AddNormal(CGrVector(0, 0, 1));
	backF->AddVertex(CGrVector(seattop1));
	backF->AddVertex(CGrVector(seattop4));
	backF->AddVertex(CGrVector(seatback2));
	backF->AddVertex(CGrVector(seatback1));
	backF->AddTriangleVertex(0,0,-1);
	backF->AddTriangleVertex(1,0,-1);
	backF->AddTriangleVertex(2,0,-1);
	backF->AddTriangleVertex(2,0,-1);
	backF->AddTriangleVertex(3,0,-1);
	backF->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(backF);

	CSgPtr<CSgMesh> backT = new CSgMesh();
	backT->AddNormal(CGrVector(0, 1, 0));
	backT->AddVertex(CGrVector(seatback3));
	backT->AddVertex(CGrVector(seatback1));
	backT->AddVertex(CGrVector(seatback2));
	backT->AddVertex(CGrVector(seatback4));
	backT->AddTriangleVertex(0,0,-1);
	backT->AddTriangleVertex(1,0,-1);
	backT->AddTriangleVertex(2,0,-1);
	backT->AddTriangleVertex(2,0,-1);
	backT->AddTriangleVertex(3,0,-1);
	backT->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(backT);

	CSgPtr<CSgMesh> backBehind = new CSgMesh();
	backBehind->AddNormal(CGrVector(0, 0, -1));
	backBehind->AddVertex(CGrVector(seatback3));
	backBehind->AddVertex(CGrVector(seatback4));
	backBehind->AddVertex(CGrVector(seatback6));
	backBehind->AddVertex(CGrVector(seatback5));
	backBehind->AddTriangleVertex(0,0,-1);
	backBehind->AddTriangleVertex(1,0,-1);
	backBehind->AddTriangleVertex(2,0,-1);
	backBehind->AddTriangleVertex(2,0,-1);
	backBehind->AddTriangleVertex(3,0,-1);
	backBehind->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(backBehind);

	CSgPtr<CSgMesh> backR = new CSgMesh();
	backR->AddNormal(CGrVector(-1, 0, 0));
	backR->AddVertex(CGrVector(seattop1));
	backR->AddVertex(CGrVector(seatback1));
	backR->AddVertex(CGrVector(seatback3));
	backR->AddVertex(CGrVector(seatback5));
	backR->AddVertex(CGrVector(seatbot1));
	backR->AddTriangleVertex(0,0,-1);
	backR->AddTriangleVertex(1,0,-1);
	backR->AddTriangleVertex(2,0,-1);
	backR->AddTriangleVertex(2,0,-1);
	backR->AddTriangleVertex(3,0,-1);
	backR->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(backR);

	CSgPtr<CSgMesh> backL = new CSgMesh();
	backL->AddNormal(CGrVector(1, 0, 0));
	backL->AddVertex(CGrVector(seattop4));
	backL->AddVertex(CGrVector(seatbot4));
	backL->AddVertex(CGrVector(seatback6));
	backL->AddVertex(CGrVector(seatback4));
	backL->AddVertex(CGrVector(seatback2));
	backL->AddTriangleVertex(0,0,-1);
	backL->AddTriangleVertex(1,0,-1);
	backL->AddTriangleVertex(2,0,-1);
	backL->AddTriangleVertex(2,0,-1);
	backL->AddTriangleVertex(3,0,-1);
	backL->AddTriangleVertex(0,0,-1);
	m_seats->AddChild(backL);


	// arm bars
	CSgPtr<CSgOctagonalCylinder> oct  = new CSgOctagonalCylinder(0.21f,0.025f);
	CSgPtr<CSgOctagonalCylinder> oct2 = new CSgOctagonalCylinder(0.21f,0.025f);
	CSgPtr<CSgOctagonalCylinder> oct3 = new CSgOctagonalCylinder(0.52f,0.025f);
	CSgPtr<CSgOctagonalCylinder> oct4 = new CSgOctagonalCylinder(0.52f,0.025f);

	CSgPtr<CSgTranslate> tr1 = new CSgTranslate(-0.25f,0.13f,0.25f);
	tr1->SetChild(oct);
	CSgPtr<CSgSeparator> sep1 = new CSgSeparator();
	sep1->SetChild(tr1);

	CSgPtr<CSgTranslate> tr2 = new CSgTranslate(0.25f,0.13f,0.25f);
	tr2->SetChild(oct2);
	CSgPtr<CSgSeparator> sep2 = new CSgSeparator();
	sep2->SetChild(tr2);

	
	CSgPtr<CSgRotate> r3 = new CSgRotate(90.0f,1,0,0);
	r3->SetChild(oct3);
	CSgPtr<CSgTranslate> tr3 = new CSgTranslate(-0.25f,0.235f,0.01f);
	tr3->SetChild(r3);
	CSgPtr<CSgSeparator> sep3 = new CSgSeparator();
	sep3->SetChild(tr3);

	CSgPtr<CSgRotate> r4 = new CSgRotate(90.0f,1,0,0);
	r4->SetChild(oct4);	
	CSgPtr<CSgTranslate> tr4 = new CSgTranslate(0.26f,0.235f,0.01f);
	tr4->SetChild(r4);
	CSgPtr<CSgSeparator> sep4 = new CSgSeparator();
	sep4->SetChild(tr4);

	CSgPtr<CSgComposite> bars = new CSgComposite();

	bars->AddChild(sep1);
	bars->AddChild(sep2);
	bars->AddChild(sep3);
	bars->AddChild(sep4);

	CSgPtr<CSgMaterial> barMat = new CSgMaterial(50.0f, 0.549f, 0.549f, 0.549f, 1.0f, 0.549f, 0.549f, 0.549f, 1.0f);
	barMat->SetChild(bars);
	CSgPtr<CSgSeparator> separ = new CSgSeparator();
	separ->SetChild(barMat);
	m_seats->AddChild(separ);



	return m_seats;
}

CSgPtr<CSgComposite> YoYoFactory::MakeChains(int type)
{
	CSgPtr<CSgComposite> m_chains = new CSgComposite();

	CSgPtr<CSgOctagonalCylinder> oct1 = new CSgOctagonalCylinder(5.9f,0.0125f);
	CSgPtr<CSgOctagonalCylinder> oct2 = new CSgOctagonalCylinder(6.1f,0.0125f);
	CSgPtr<CSgOctagonalCylinder> oct3 = new CSgOctagonalCylinder(5.9f,0.0125f);
	CSgPtr<CSgOctagonalCylinder> oct4 = new CSgOctagonalCylinder(6.1f,0.0125f);

	// Seat positioned below beam
	if (type == 0)
	{
		CSgPtr<CSgRotate> rot1 = new CSgRotate(0.0f,1,0,0);
		rot1->SetChild(oct1);
		CSgPtr<CSgRotate> rot2 = new CSgRotate(-2.0f,0,0,1);
		rot2->SetChild(rot1);	
		CSgPtr<CSgTranslate> tr1 = new CSgTranslate(-0.16f,3.4f,-0.26f);
		tr1->SetChild(rot2);
		CSgPtr<CSgSeparator> sep1 = new CSgSeparator();
		sep1->SetChild(tr1);
		m_chains->AddChild(sep1);


		CSgPtr<CSgRotate> rot3 = new CSgRotate(0.0f,1,0,0);
		rot3->SetChild(oct1);
		CSgPtr<CSgRotate> rot4 = new CSgRotate(2.0f,0,0,1);
		rot4->SetChild(rot3);	
		CSgPtr<CSgTranslate> tr2 = new CSgTranslate(0.16f,3.4f,-0.26f);
		tr2->SetChild(rot4);
		CSgPtr<CSgSeparator> sep2 = new CSgSeparator();
		sep2->SetChild(tr2);
		m_chains->AddChild(sep2);

		CSgPtr<CSgRotate> rot5 = new CSgRotate(0.0f,1,0,0);
		rot5->SetChild(oct2);
		CSgPtr<CSgRotate> rot6 = new CSgRotate(2.0f,0,0,1);
		rot6->SetChild(rot5);	
		CSgPtr<CSgTranslate> tr3 = new CSgTranslate(0.16f,3.3f,0.26f);
		tr3->SetChild(rot6);
		CSgPtr<CSgSeparator> sep3 = new CSgSeparator();
		sep3->SetChild(tr3);
		m_chains->AddChild(sep3);


		CSgPtr<CSgRotate> rot7 = new CSgRotate(0.0f,1,0,0);
		rot7->SetChild(oct2);
		CSgPtr<CSgRotate> rot8 = new CSgRotate(-2.0f,0,0,1);
		rot8->SetChild(rot7);	
		CSgPtr<CSgTranslate> tr4 = new CSgTranslate(-0.16f,3.3f,0.26f);
		tr4->SetChild(rot8);
		CSgPtr<CSgSeparator> sep4 = new CSgSeparator();
		sep4->SetChild(tr4);
		m_chains->AddChild(sep4);
	}
	else if (type == 1)
	{
		CSgPtr<CSgRotate> rot1 = new CSgRotate(-4.8f,1,0,0);
		rot1->SetChild(oct3);
		CSgPtr<CSgRotate> rot2 = new CSgRotate(-2.0f,0,0,1);
		rot2->SetChild(rot1);	
		CSgPtr<CSgTranslate> tr1 = new CSgTranslate(-0.16f,3.4f,-0.51f);
		tr1->SetChild(rot2);
		CSgPtr<CSgSeparator> sep1 = new CSgSeparator();
		sep1->SetChild(tr1);
		m_chains->AddChild(sep1);


		CSgPtr<CSgRotate> rot3 = new CSgRotate(-4.8f,1,0,0);
		rot3->SetChild(oct3);
		CSgPtr<CSgRotate> rot4 = new CSgRotate(2.0f,0,0,1);
		rot4->SetChild(rot3);	
		CSgPtr<CSgTranslate> tr2 = new CSgTranslate(0.16f,3.4f,-0.51f);
		tr2->SetChild(rot4);
		CSgPtr<CSgSeparator> sep2 = new CSgSeparator();
		sep2->SetChild(tr2);
		m_chains->AddChild(sep2);

		CSgPtr<CSgRotate> rot5 = new CSgRotate(4.8f,1,0,0);
		rot5->SetChild(oct4);
		CSgPtr<CSgRotate> rot6 = new CSgRotate(2.0f,0,0,1);
		rot6->SetChild(rot5);	
		CSgPtr<CSgTranslate> tr3 = new CSgTranslate(0.16f,3.3f,0.51f);
		tr3->SetChild(rot6);
		CSgPtr<CSgSeparator> sep3 = new CSgSeparator();
		sep3->SetChild(tr3);
		m_chains->AddChild(sep3);


		CSgPtr<CSgRotate> rot7 = new CSgRotate(4.8f,1,0,0);
		rot7->SetChild(oct4);
		CSgPtr<CSgRotate> rot8 = new CSgRotate(-2.0f,0,0,1);
		rot8->SetChild(rot7);	
		CSgPtr<CSgTranslate> tr4 = new CSgTranslate(-0.16f,3.3f,0.51f);
		tr4->SetChild(rot8);
		CSgPtr<CSgSeparator> sep4 = new CSgSeparator();
		sep4->SetChild(tr4);
		m_chains->AddChild(sep4);
	}

	return m_chains;
}

CSgPtr<CSgComposite> YoYoFactory::MakeTop()
{
	// composite that we'll return
	CSgPtr<CSgComposite> m_top = new CSgComposite();

	/*******************
	Happy guy
	*******************/
	CSgPtr<CSgPolygon> top = new CSgPolygon();
	top->AddNormal(CGrVector(0, 0, 1));
	top->AddTexCoord(CGrVector(0.95,0.725));
	top->AddVertex(CGrVector(0.45,0.225,0.0));
	top->AddTexCoord(CGrVector(0.725,0.95));
	top->AddVertex(CGrVector(0.225,0.45,0.0));
	top->AddTexCoord(CGrVector(0.275,0.95));
	top->AddVertex(CGrVector(-0.225,0.45,0.0));
	top->AddTexCoord(CGrVector(0.05,0.725));
	top->AddVertex(CGrVector(-0.45,0.225,0.0));
	top->AddTexCoord(CGrVector(0.05,0.275));
	top->AddVertex(CGrVector(-0.45,-0.225,0.0));
	top->AddTexCoord(CGrVector(0.275,0.05));
	top->AddVertex(CGrVector(-0.225,-0.45,0.0));
	top->AddTexCoord(CGrVector(0.725,0.05));
	top->AddVertex(CGrVector(0.275,-0.45,0.0));
	top->AddTexCoord(CGrVector(0.95,0.275));
	top->AddVertex(CGrVector(0.45,-0.225,0.0));

	CSgPtr<CSgTexture> tex = new CSgTexture(m_happy);
	tex->SetChild(top);
	CSgPtr<CSgTranslate> trans = new CSgTranslate(0.0,1.25,0.08);
	trans->SetChild(tex);
	CSgPtr<CSgSeparator> sep = new CSgSeparator();
	sep->SetChild(trans);

	CSgPtr<CSgPolygon> top2 = new CSgPolygon();
	top2->AddNormal(CGrVector(0, 0, -1));
	top2->AddTexCoord(CGrVector(0.95,0.725));
	top2->AddVertex(CGrVector(0.45,0.225,0.0));
	top2->AddTexCoord(CGrVector(0.95,0.275));
	top2->AddVertex(CGrVector(0.45,-0.225,0.0));
	top2->AddTexCoord(CGrVector(0.725,0.05));
	top2->AddVertex(CGrVector(0.225,-0.45,0.0));
	top2->AddTexCoord(CGrVector(0.275,0.05));
	top2->AddVertex(CGrVector(-0.225,-0.45,0.0));
	top2->AddTexCoord(CGrVector(0.05,0.275));
	top2->AddVertex(CGrVector(-0.45,-0.225,0.0));
	top2->AddTexCoord(CGrVector(0.05,0.725));
	top2->AddVertex(CGrVector(-0.45,0.225,0.0));
	top2->AddTexCoord(CGrVector(0.275,0.95));
	top2->AddVertex(CGrVector(-0.225,0.45,0.0));
	top2->AddTexCoord(CGrVector(0.725,0.95));
	top2->AddVertex(CGrVector(0.225,0.45,0.0));

	CSgPtr<CSgTexture> tex2 = new CSgTexture(m_happy);
	tex2->SetChild(top2);
	CSgPtr<CSgTranslate> trans2 = new CSgTranslate(0.0,1.25,-0.08);
	trans2->SetChild(tex2);
	CSgPtr<CSgSeparator> sep2 = new CSgSeparator();
	sep2->SetChild(trans2);

	/*******************
	// octagon for beam to support faces & other oct.
	*******************/
	
	CSgPtr<CSgMaterial> grnmat2 = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	CSgPtr<CSgOctagonalCylinder> oct_tower = new CSgOctagonalCylinder(1.0f,0.075f, grnmat2);
	CSgPtr<CSgTranslate> trans3 = new CSgTranslate(0.0,0.5,0.0);
	trans3->SetChild(oct_tower);
	CSgPtr<CSgSeparator> sep3 = new CSgSeparator();
	sep3->SetChild(trans3);
	
	/*******************
	// octagon for faces
	*******************/
	CSgPtr<CSgMaterial> grnmat1 = new CSgMaterial(50.0f, 0.1255f, 0.753f, 0.251f, 1.0f, 0.1255f, 0.753f, 0.251f, 1.0f);
	CSgPtr<CSgOctagonalCylinder> oct_face = new CSgOctagonalCylinder(0.15f, 0.5f, grnmat1);
	CSgPtr<CSgRotate> rot2 = new CSgRotate(90.0f,1,0,0);
	rot2->SetChild(oct_face);
	CSgPtr<CSgTranslate> trans4 = new CSgTranslate(0.0,1.25f,0.0f);
	trans4->SetChild(rot2);
	CSgPtr<CSgSeparator> sep4 = new CSgSeparator();
	sep4->SetChild(trans4);



	m_top->AddChild(sep);
	m_top->AddChild(sep2);
	m_top->AddChild(sep3);
	m_top->AddChild(sep4);

	return m_top;
}